package com.smartwebee.android.blespp.hospital.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.databinding.AdapterPlanChildBinding;
import com.smartwebee.android.blespp.databinding.AdapterPlanParentBinding;
import com.smartwebee.android.blespp.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by steward on 2018/7/5.
 */

public class PlanExpandableAdapter extends BaseExpandableListAdapter {
    public List<String> groupItem = Arrays.asList("锻炼计划一", "锻炼计划二", "锻炼计划三");
    private List<ArrayList<String>> childItem = Arrays.asList(new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>());
    private Context context;

    public PlanExpandableAdapter(Context context) {
        this.context = context;
    }

    public void addChildItem(int groupPosition, String childItem) {
        this.childItem.get(groupPosition).add(childItem);
        notifyDataSetChanged();
    }

    public void addChildItems(List<ArrayList<String>> mChildItem) {
        for (int i = 0; i < groupItem.size(); i++) {
            this.childItem.get(i).addAll(mChildItem.get(i));
        }
        notifyDataSetChanged();
    }

    public List<ArrayList<String>> getChildItem() {
        return childItem;
    }

    public void clearItems() {
        for (int i = 0; i < groupItem.size(); i++) {
            this.childItem.get(i).clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return 3;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childItem.get(groupPosition).size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return groupItem.get(groupPosition);
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        return childItem.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        AdapterPlanParentBinding binding;
        if (convertView == null) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.adapter_plan_parent, parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (AdapterPlanParentBinding)convertView.getTag();
        }
        binding.tvName.setText(groupItem.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        AdapterPlanChildBinding binding;
        if (convertView == null) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.adapter_plan_child, parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (AdapterPlanChildBinding)convertView.getTag();
        }
        binding.tvName.setText(Utils.getModeName(childItem.get(groupPosition).get(childPosition)));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
